package com.wind.activity;

import static com.xuexiang.xupdate.XUpdate.getContext;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;


import com.wind.base.BaseFragment;
import com.wind.fragment.HomeFragment;
import com.wind.fragment.MeFragment;
import com.wind.fragment.OceanFragment;
import com.wind.fragment.StarFragment;
import com.debug.soblogin.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.xuexiang.xupdate.XUpdate;

public class navActivity extends AppCompatActivity {
    private HomeFragment mHomeFragment;
    private OceanFragment mOceanFragment;
    private StarFragment mStarFragment;
    private MeFragment mMeFragment;
    private FragmentManager mFm;
    public BottomNavigationView mNavigationView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav);
        mNavigationView = findViewById(R.id.main_navigation_bar);
        initFragment();
        initListener();
        //加载完后，开始检查版本是否需要更新
        XUpdate.newBuild(getContext())
                .updateUrl("http://mp.lolxiaoguo.cn/apk/wind_update.json?r=" + System.currentTimeMillis())
                .update();
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        //super.onSaveInstanceState(outState, outPersistentState);
        super.onSaveInstanceState(outState);
    }

    private void initFragment() {
        mHomeFragment = new HomeFragment();
        mOceanFragment = new OceanFragment();
        mStarFragment = new StarFragment();
        mMeFragment = new MeFragment();
        mFm = getSupportFragmentManager();
        switchFragment(mHomeFragment);
    }

    private void initListener() {
        mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.home) {
                    switchFragment(mHomeFragment);
                } else if (item.getItemId() == R.id.ocean) {
                    switchFragment(mOceanFragment);
                } else if (item.getItemId() == R.id.star) {
                    switchFragment(mStarFragment);
                } else if (item.getItemId() == R.id.me) {
                    switchFragment(mMeFragment);
                    //


                }
                return true;
            }
        });




    }




    /**
     * 上一次显示的fragment
     */
    private BaseFragment lastOneFragment = null;

    private void switchFragment(BaseFragment targetFragment) {
        //如果上一个fragment与当前要切换的fragment是同一个，则不需要切换
        if (lastOneFragment == targetFragment) {
            return;
        }
        //修改成add和hide的方是来控制fragment的切换
        FragmentTransaction fragmentTransaction = mFm.beginTransaction();
        if (!targetFragment.isAdded()) {

            fragmentTransaction.add(R.id.main_page_container, targetFragment);
        } else {
            fragmentTransaction.show(targetFragment);
        }
        if (lastOneFragment != null) {
            fragmentTransaction.hide(lastOneFragment);
        }
        lastOneFragment = targetFragment;
        fragmentTransaction.commit();
    }

}
